﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace INotifyCollectionChanged_sample {
  class Program {

    static void Main(string[] args) {
      ObservableCollection<int> oc = new ObservableCollection<int>();
      oc.CollectionChanged += OnCollectionChanged;
      Console.WriteLine("--- Add ---");
      oc.Add(1); 
      Console.WriteLine("--- Replace ---");
      oc[0] = 2; 
      Console.WriteLine("--- Remove ---");
      oc.RemoveAt(0);
      Console.WriteLine("--- Clear ---");
      oc.Clear();
    }

    static void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
      Console.WriteLine("Action : {0}", e.Action);
      Console.WriteLine("NewStartingIndex : {0}", e.NewStartingIndex);
      Console.Write("NewItems : ");
      if ( e.NewItems != null ) { foreach( object item in e.NewItems ) Console.Write("{0} ", item); } 
      else { Console.Write("(null)"); } Console.WriteLine();
      Console.WriteLine("OldStartingIndex : {0}", e.OldStartingIndex);
      Console.Write("OldItems : ");
      if ( e.OldItems != null ) { foreach( object item in e.OldItems ) Console.Write("{0} ", item); } 
      else { Console.Write("(null)"); } Console.WriteLine();
    }

  }
}

/* 実行結果
--- Add ---
Action : Add
NewStartingIndex : 0
NewItems : 1
OldStartingIndex : -1
OldItems : (null)
--- Replace ---
Action : Replace
NewStartingIndex : 0
NewItems : 2
OldStartingIndex : 0
OldItems : 1
--- Remove ---
Action : Remove
NewStartingIndex : -1
NewItems : (null)
OldStartingIndex : 0
OldItems : 2
--- Clear ---
Action : Reset
NewStartingIndex : -1
NewItems : (null)
OldStartingIndex : -1
OldItems : (null)
*/
